//
//  LukyMoneyDetailContentCell.m
//  meiyinxiang
//
//  Created by apple on 2017/11/9.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "LukyMoneyDetailContentCell.h"
#import "UIImage+Category.h"
#import "JX_GCDTimerManager.h"
#import "PictureImageView.h"
#import "CommentView.h"
#define GCDDispatchName @"GCDDispatchNameTimer"
@interface LukyMoneyDetailContentCell(){
    NSInteger countDown;
}
@property (weak, nonatomic) IBOutlet UILabel *yuanLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureViewHeightCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *moneyLabelHeightCons;
@property (weak, nonatomic) IBOutlet UIImageView *iconImagView;
@property (weak, nonatomic) IBOutlet UILabel *contenLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIView *timeLabelBgView;
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
@property (weak, nonatomic) IBOutlet UIImageView *topImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *backTopLayoutCons;
@property (weak, nonatomic) IBOutlet UILabel *fromLabel;
@property (weak, nonatomic) IBOutlet PictureImageView *pictureView;
@property (weak, nonatomic) IBOutlet CommentView *commentView;
@property (weak, nonatomic) IBOutlet UILabel *receivedMoneyLabel;

@end
@implementation LukyMoneyDetailContentCell
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
  
    self.iconImagView.contentMode = UIViewContentModeScaleAspectFill;
    self.iconImagView.layer.cornerRadius = 30;
    self.iconImagView.layer.masksToBounds = YES;
    if (IS_IPHONE_X) {
        _backTopLayoutCons.constant = 30;
    }
    //添加图片查看功能
    [self addImgTapGes];
//    11月29 区分个人中心我的发布  1，改了icon top居下79  2.增加moneyView  3，是否有倒计时
   // [self addADTimer];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}
- (void)addImgTapGes{
    //点击查看照片
    UITapGestureRecognizer *tapImg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImgAct)];
    [self.contenImageView addGestureRecognizer:tapImg];
    self.contenImageView.userInteractionEnabled = YES;
}
- (void)tapImgAct{
    HMPhotoBrowserController *photoBrowser =   [HMPhotoBrowserController photoBrowserWithSelectedIndex:0 urls:@[self.detailModel.file] parentImageViews:@[self.contenImageView]];
    [self.mSuperVC presentViewController:photoBrowser animated:YES completion:nil];
}
- (void)setDetailModel:(MYXDetailViewModel *)detailModel{
    _detailModel = detailModel;
    self.contenLabel.text = detailModel.content;
    [self.iconImagView sd_setImageWithURL:[NSURL URLWithString:detailModel.userface] placeholderImage:[UIImage imageNamed:@"AppIcon"]];
    self.iconImagView.layer.cornerRadius = 30;
    self.iconImagView.layer.masksToBounds = YES;
     self.fromLabel.text = [NSString stringWithFormat:@"%@的信息红包",detailModel.username];
    self.pictureViewHeightCons.constant = detailModel.pictureViewSize.height;
    WS(weakSelf);
   if (detailModel.singleDownloaderFinished || detailModel.file.count > 1) {//单张图片下载完成
        weakSelf.pictureView.detailModel = weakSelf.detailModel;
       weakSelf.pictureView.mSuperVC = weakSelf.mSuperVC;
    }
    if ([detailModel.isreceived isEqualToString:@"1"]) {// 抢到红包金额显示
        self.receivedMoneyLabel.text = detailModel.my_money;
    }
    self.commentView.detailModel = detailModel;
    self.commentView.mSuperVC = self.mSuperVC;
    //self.pictureView.detailModel = detailModel;
//    if (_detailModel.file.length > 0) {
//        [self.contenImageView sd_setImageWithURL:[NSURL URLWithString:_detailModel.file] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//            NSLog(@"imageView size is %f,%f",image.size.height,image.size.width);
//            if (image) {
//                [self resizeConsHeightAndWidthWithImg:image];
//                self.contenImageView.image = image;
//            }else{
//                self.heightCons.constant = 0.01;
//            }
//
//        }];
//    } else{
//        self.heightCons.constant = 0.01;
//    }
//判断是否是个人中心发布详情
    if ([detailModel.isController isEqualToString:@"1"]) {
        if ([detailModel.restcount isEqualToString:detailModel.rcount]) {
            self.timeLabel.text = @"已领完";
        }else{
             self.timeLabel.text = @"未领完";
        }
        self.receivedMoneyLabel.hidden = YES;
        self.moneyLabelHeightCons.constant = 0;
//        self.yuanLabel.hidden = YES;
//        self.commentView.hidden = YES;
        //self.commentView.commendBtn.enabled = NO;
        self.fromLabel.hidden = YES;
        self.moneyView.hidden = NO;
        self.topImageView.image =[UIImage imageNamed:@"Publishtuoyuan"];
        self.bgheightCons.constant = 110;
        self.moneyLabel.text =detailModel.money;
    }else{
        self.moneyView.hidden = YES;
        self.bgheightCons.constant = 110;
        self.topImageView.image =[UIImage imageNamed:@"tuoyuan"];
        if ([detailModel.isreceived isEqualToString:@"0"] && !detailModel.isSecondRefresh) {//未领取红包
            self.moneyLabelHeightCons.constant = 0;
            [self addADTimer];
        }else if ([detailModel.isreceived isEqualToString:@"1"]){
            self.timeLabel.hidden = YES;
            self.timeLabelBgView.hidden = YES;
            self.moneyLabelHeightCons.constant = 42;
        }else if ([detailModel.isreceived isEqualToString:@"0"] && detailModel.isSecondRefresh){
            self.timeLabel.hidden = YES;
            self.timeLabelBgView.hidden = YES;
            self.moneyLabelHeightCons.constant = 0;
        }
    }
}
+ (LukyMoneyDetailContentCell *)loadNibWithLukyMoneyDetailContentCell{
    UINib *nib = [UINib nibWithNibName:@"LukyMoneyDetailContentCell" bundle:[NSBundle mainBundle]];
    
    return [[nib instantiateWithOwner:nil options:nil] lastObject];
}
//- (void)resizeConsHeightAndWidthWithImg:(UIImage *)image{
//    CGFloat imageHeight = image.size.height;
//    CGFloat imageWidth = image.size.width;
//    if (!image) {
//        imageHeight = 1;
//        imageWidth = 1;
//    }
//    if (imageWidth <  imageHeight) {
//         _heightCons.constant = _withCons.constant/(imageWidth/imageHeight);
//
//    }else{
//        _withCons.constant = ScreenWidth - 20;
//        _heightCons.constant = _withCons.constant/(imageWidth/imageHeight);
//
//    }
//}
#pragma mark -PrivateMethod/Users/apple
- (void)addADTimer{
    countDown = 5;
    self.timeLabelBgView.layer.cornerRadius = 10;
    self.timeLabelBgView.layer.masksToBounds = YES;
    __weak typeof(self)weakself = self;
    [[JX_GCDTimerManager sharedInstance] scheduledDispatchTimerWithName:GCDDispatchName timeInterval:1.0 queue:nil repeats:YES actionOption:AbandonPreviousAction action:^{
        countDown --;
        dispatch_async(dispatch_get_main_queue(), ^{
        weakself.timeLabel.text = [NSString stringWithFormat:@"%ldS领取",countDown];
        });
       
        if (countDown <= 0) {
            weakself.detailModel.isreceived = @"1";
            //5秒后请求抢红包接口
            dispatch_async(dispatch_get_main_queue(), ^{//主线层
               // weakself.timeLabel.hidden = YES;
                [self.detailModel getLuckyMoneywithCompletion:^(BOOL success, NSString *message) {
                    if (success) {
                        if (weakself.adfinshBlock) {
                            weakself.adfinshBlock();
                        }
                        NSLog(@"请求成功");
                    }else{
                        weakself.adfinshBlock();
                        ShowSVProgressErrorMessage(message);
                        //   [SVProgressHUD showErrorWithStatus:message];
                    }
                    [[NSNotificationCenter defaultCenter] postNotificationName:Post5SecondSucessNotificationName object:nil];
                }];
            });
           
            [[JX_GCDTimerManager sharedInstance] cancelTimerWithName:GCDDispatchName];
        }
    }];
}
- (void)deleteTimer{
    [[JX_GCDTimerManager sharedInstance] cancelTimerWithName:GCDDispatchName];

}

//- (void)removeFromSuperview{
//    [super removeFromSuperview];
//[[JX_GCDTimerManager sharedInstance] cancelTimerWithName:GCDDispatchName];
//
//}

@end
